<?php

namespace Lims\UserManagementBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Utils\SecurityBundle\CustomClass\SecurityFunctions;


class UserManagementController extends Controller
{
    public function loginAction()
    {
        // last username entered by the user
        $lastUsername = $this->getRequest()->getSession()->get('lastUserName');
        
        return $this->render('LimsUserManagementBundle:UserManagement:login.html.twig', array(
            'lastUsername' => $lastUsername
            ));
    }
    
    public function loginExecuteAction(Request $request)
    {
//        echo sha1('user1'.'a');die;
        if($request->isMethod('POST'))
        {
            $username = SecurityFunctions::clear($request->request->get('username'));
            $password = $request->request->get('password');
            
            $user = $this->getDoctrine()->getManager()->getRepository('LimsMainBundle:Users')->login($username, $password);            
            
            if(!$user)            
            {
                $this->get('session')->getFlashBag()->add('error', 'Нэвтрэх нэр / Нууц үг буруу байна.');
                $this->getRequest()->getSession()->set('lastUserName', $username);
                return new RedirectResponse($this->generateUrl('lims_user_management_login'));
                
            }else{
                
                $token = new UsernamePasswordToken($user, $user->getPassword(), "users", array('ROLE_USER'));                
                $this->container->get('security.context')->setToken($token);
                $this->get('event_dispatcher')->dispatch(AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($token));
                $this->get('session')->set('_security_users', serialize($token));
                
                $this->get('session')->getFlashBag()->add('notice', 'Сайн байна уу '.$user->getUsername());
                return new RedirectResponse($this->generateUrl('lims_homepage'));
            }
            
            
        }else{
            $this->get('session')->getFlashBag()->add('error', 'Нэвтрэх нэр / Нууц үг буруу байна.');
            $this->getRequest()->getSession()->set('lastUserName', $username);
            return new RedirectResponse($this->generateUrl('lims_user_management_login'));
        }
        
    }
    
    public function exitAction()
    {
        $this->get('security.context')->setToken(null);
        $this->get('request')->getSession()->invalidate();     
        return new RedirectResponse($this->generateUrl('lims_user_management_login'));
    }
    
    
}
